package net.koodar.forge.system.support.loginLog.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import net.koodar.forge.mybatis.util.PageUtil;
import net.koodar.forge.system.support.loginLog.model.dto.LoginLogQueryDTO;
import net.koodar.forge.system.support.loginLog.model.entity.LoginLog;
import net.koodar.forge.system.support.loginLog.dao.LoginLogMapper;
import net.koodar.forge.common.domain.dto.Response;
import net.koodar.forge.common.domain.dto.SingleResponse;
import net.koodar.forge.log.event.LoginLogEvent;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liyc
 */
@Service
@RequiredArgsConstructor
public class LoginLogServiceImpl implements LoginLogService {

	private final LoginLogMapper loginLogMapper;

	@Override
	public Response pageBy(LoginLogQueryDTO loginLogQuery) {
		Page<LoginLog> loginLogPage = loginLogMapper.selectPage(loginLogQuery.toPage(), loginLogQuery.toQueryWrapper());
		List<LoginLog> records = loginLogPage.getRecords();
		return SingleResponse.ok(PageUtil.toPage(records, loginLogPage.getTotal()));
	}

	@Override
	public void add(LoginLogEvent event) {
		LoginLog loginEntity = LoginLog.builder()
				.userId(event.getUserId())
				.userName(event.getUserName())
				.userAgent(event.getUserAgent())
				.loginIp(event.getLoginIp())
				.remark(event.getRemark())
				.loginResult(event.getLoginResult())
				.build();
		loginLogMapper.insert(loginEntity);
	}
}
